jQuery(function(){
	//获取显示页面数据 服务器分页
    var serverData = {};
    serverData.key =''
    var serverTable;
    jQuery(document).ready(function(){
    	 serverTable = jQuery('#wholeSetTable').DataTable({
             "serverSide": true,
             "ordering": false,
             "ajax": {
                 "dataType": "json",
                 "type": "get",
                 "cache": false,
                 "url":window.rootPath+ '/ui/wholeSetDevice/getpage',
                 "data": function (d) {
                     return $.extend({}, d, serverData);
                 }
             },
             columns: [
   	                {
   	                	data: null
   	                },
   	                {
 	                    data: null,
 	                    defaultContent: "",
 	                    render: function (data, type, full, meta) {
 	                        var id=data.id
 	                        var operate ='<input type="checkbox" class="checkchildReplace">';
 	                        return operate;
 	                    }
   	                },
   	                {
   	                    data: 'deviceName',
		                render:function(data,type,full,meta){
		                	if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 25) ? '<font title="'+data+'">'+data.substring(0,25)+'...'+'</font>' : data;
							}
						}
   	                },
   	                {
   	                    data: 'type',
		                render:function(data,type,full,meta){
		                	if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 25) ? '<font title="'+data+'">'+data.substring(0,25)+'...'+'</font>' : data;
							}
						}
   	                },
   	                {
   	                    data: 'deviceSize'
   	                },
   	                {
   	                    data: 'weight'
   	                },
   	                {
   	                    data: 'deliveryTime'
   	                },
   	                {
   	                    data: 'attachNumber',
   	                    render: function (data, type, full, meta) {
   	                    	var dOut;
   	                    	if(data==null){
   	                    		dOut='<td class="center" ><span class="badge label-pill label-primary">'+0+'</span></td>'
   	                    	}else{
   	                    		dOut='<td class="center" ><span class="badge label-pill label-primary">'+data+'</span></td>'
   	                    	}
   	                    	return dOut;
   	                    }
   	                    
   	                }  
   	               ],
             "drawCallback": function (oSettings) {
                 var k = 0;
                 var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                 for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                     $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                     k++;
                 } 
             }
         });
    })
    
  //删除备品备件设备
    jQuery('.delBtn').on('click', function () {
        var selects=getSelect("#wholeSetTable",1);
        var url=window.rootPath+"/ui/wholeSetDevice/delWholeSetDevice";
        delSelect(selects,url,serverTable)
    });

    //添加销售人员属性，弹窗清空
	 jQuery('.addBtn').on('click', function () {
		jQuery('#id').val('');
		jQuery('#deviceName').val('');
		jQuery('#attachNumber').val('');
		jQuery('#type').val('');
		jQuery('#weight').val('');
		jQuery('#size').val('');
		jQuery('#deliveryTime').val('');
		jQuery('#memo').val('');
		jQuery('#wholeSetDeviceModal').modal('show');
	});

     //编辑成套设备信息
    jQuery('.editBtn').on('click', function () {
        var selects=getSelect("#wholeSetTable",1);
        if(selects.length>1){
            $.springLemonDialog({
                content: '只能编辑一条记录',// 确认文本
                clickButton: function (sender, modal, index) {// 按钮事件处理
                    $(this).closeDialog(modal);
                }
            });
        }
        else if(selects.length==0){
            $.springLemonDialog({
                content: '请选择要编辑的记录',// 确认文本
                clickButton: function (sender, modal, index) {// 按钮事件处理
                    $(this).closeDialog(modal);
                }
            });
        }else if(selects.length==1) {
            jQuery('#id').val(selects[0].id);
            jQuery('#attachNumber').val(selects[0].attachNumber);
    		jQuery('#deviceName').val(selects[0].deviceName);
    		jQuery('#type').val(selects[0].type);
    		jQuery('#weight').val(selects[0].weight);
    		jQuery('#size').val(selects[0].deviceSize);
    		jQuery('#deliveryTime').val(selects[0].deliveryTime);
    		jQuery('#memo').val(selects[0].memo);
    		jQuery('#wholeSetDeviceModal').modal('show');
        };
    });
  //上传附件
    jQuery('.upLoadBtn').on('click', function () {
        var selects=getSelect("#wholeSetTable",1);
        if(selects.length>1){
            $.springLemonDialog({
                content: '只能对一条记录上传文档',// 确认文本
                clickButton: function (sender, modal, index) {// 按钮事件处理
                    $(this).closeDialog(modal);
                }
            });
        }
        else if(selects.length==0){
            $.springLemonDialog({
                content: '请选择要上传文档的记录',// 确认文本
                clickButton: function (sender, modal, index) {// 按钮事件处理
                    $(this).closeDialog(modal);
                }
            });
        }else if(selects.length==1) {
        	var deviceId=selects[0].id;
        	var fileType=selects[0].deviceName;
        	var url='/ui/wholeSetDevice';
        	fileUpload(deviceId,fileType,serverTable);  	
        };
    });
   
    
    //下载附件
    jQuery('.downLoadBtn').on('click', function () {
        var selects=getSelect("#wholeSetTable",1);
        if(selects.length>1){
            $.springLemonDialog({
                content: '只能对一条记录下载文档',// 确认文本
                clickButton: function (sender, modal, index) {// 按钮事件处理
                    $(this).closeDialog(modal);
                }
            });
        }
        else if(selects.length==0){
            $.springLemonDialog({
                content: '请选择要下载的记录',// 确认文本
                clickButton: function (sender, modal, index) {// 按钮事件处理
                    $(this).closeDialog(modal);
                }
            });
        }else if(selects.length==1) {
        	var deviceId=selects[0].id;
        	var fileType=selects[0].deviceName;
        	filedown(deviceId,fileType,'1');
        };
    });
    //列表中的全选
    $("#checkAllReplace").click(function(){
        var check = $(this).prop("checked");
        $(".checkchildReplace").prop("checked", check);
    });
    //验证型号重复
//    jQuery("#type").blur(function(){
//    	var wholeType=jQuery("#type").val();
//    	if(wholeType!=''){
//    		var url=window.rootPath+'/ui/wholeSetDevice/wholeTypeValidate'+"?type="+wholeType;
//    		jQuery.ajax({
//    			url:encodeURI(url),
//    			cache:false,
//    			type:'get',
//    			dataType:'json',
//    			success:function(data){
//    				message("型号重复");
//    			}
//    		});
//    	}
//    });
    //添加/编辑销售人员属性，保存
	var $btn = jQuery('.saveBtn').on('click', function () {
        if(jQuery("#wholeSetDeviceEditForm") && !jQuery("#wholeSetDeviceEditForm").Validform({tiptype:3, showAllError:true}).check(false)){
            return;
        };
        var isId=jQuery("#id").val();
        var deviceName=jQuery('#deviceName').val();
        var attachNumber = jQuery('#attachNumber').val();
     	var type = jQuery('#type').val();
     	var weight = jQuery('#weight').val();
 		var size = jQuery('#size').val();
 		var deliveryTime = jQuery('#deliveryTime').val();
 		var memo = jQuery('#memo').val();
 		//验证唯一性
 			var d={
 					"id":isId,
 					'type':type
 			}
 			var url=window.rootPath+'/ui/wholeSetDevice/wholeTypeValidate';
 			jQuery.ajax({
 				url:encodeURI(url),
    			type:'get',
    			data:d,
    			dataType:'json',
    			success:function(data){
    				if(data.code>0){
    					message("型号重复");
    				}else{
    					//判断是编辑还是新增
    			        if(isId == ""){
    			        	var api = window.rootPath + "/ui/wholeSetDevice/add";
    			            var d = {
    			            	"deviceName": deviceName,
    			                "type": type,
    			                "weight": weight,
    			                "deviceSize": size,
    			                "deliveryTime": deliveryTime,
    			                "memo": memo
    			            };
    			            jQuery(".btn").attr("disabled", true);
    			            jQuery.ajax({
    			                type: 'post',
    			                url: api,
    			                data: JSON.stringify(d),
    			                dataType: 'json',
    			                contentType: 'application/json;charset=utf-8',
    			                success: function (data) {
    			                	jQuery(".btn").attr("disabled", false);
    			                	jQuery('#wholeSetDeviceModal').modal('hide');
    			                    serverTable.draw();
    			                }
    			            });

    			        }
    			        else{
    			        	var data={
    			                    "id":isId,
    			                    "attachNumber":attachNumber,
    			                    "deviceName": deviceName,
    			                    "type": type,
    			                    "weight": weight,
    			                    "deviceSize": size,
    			                    "deliveryTime": deliveryTime,
    			                    "memo": memo
    			                };
    			                var url=window.rootPath+'/ui/wholeSetDevice/';
    			                jQuery(".btn").attr("disabled", true);
    			                jQuery.ajax({
    			                    url:encodeURI(url),
    			                    type:'patch',
    			                    data:JSON.stringify(data),
    			                    dataType:'json',
    			                    contentType:'application/json;charset=utf-8',
    			                    success: function () {
    			                    	jQuery(".btn").attr("disabled", false);
    			                        jQuery('#wholeSetDeviceModal').modal('hide');
    			                        serverTable.draw(false);
    			                    }

    			                });
    			        }
    				}
    			}
 			});
    });
    
	 
	//查询
    jQuery('.searchBtn').on('click',function(){
        serverData.key=jQuery('#keyword').val();
        serverTable.draw();
    })
    //撤销查询
    jQuery('.resetBtn').on("click",function()
    {
        jQuery('#keyword').val('');
        serverData.key ='';
        serverTable.draw();
    });
    
    //EXCEL导入
    jQuery('.btnImport').on("click",function()
    {
       fileUpload1();
    });
    /**
	 * 导入文件上传插件
	 */
	var fileUpload1 = function() {
		var root = window.rootPath;
		WebFilesUploader(
				{
					_webuploader : {
						server : root + '/ui/wholeSetDevice/import',
						chunked:false		
					}
				
				}, function(jsons) {    				
					 window.location.href=root+'/ui/wholeSetDevice';
				});
	};
	///excel导出
	jQuery('.btnExport').on("click",function() {
		 var selects=getSelect("#wholeSetTable",1);
		if(selects.length>0){
			var key=selects[0].id;
			for(var i=1;i<selects.length;i++){
				   key += ',' + selects[i].id;  
			 }
			   var url=window.rootPath+ "/ui/wholeSetDevice/exportSelects/"+key;
			   window.location = url
			
		}else{
			var key = jQuery('#keyword').val();
			if (!key)
				key = 'null';
			var url = window.rootPath+ "/ui/wholeSetDevice/export/" + key;
			window.location = url;
		}
	});
	
	
	
	
})
   
